package com.yurendao.shop.aspect.user;

import com.yurendao.shop.user.domain.User;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

/**
 * Created by kong on 15/10/6.
 */
@Aspect
public class UserServiceAOP {

    private Md5PasswordEncoder encoder = new Md5PasswordEncoder();
    private Logger logger = Logger.getLogger(UserServiceAOP.class);

    /**
     * MD5加盐加密
     * @param joinPoint
     */
    @Before("execution(* com.yurendao.shop.user.service.*.insert*(..))")
    public void encode(JoinPoint joinPoint){
        if(joinPoint.getArgs()[0] instanceof User){
            User user = (User) joinPoint.getArgs()[0];
            String upass = user.getPassword();
            String uname = user.getNickname();
            String encodepass = encoder.encodePassword(uname,uname);
            user.setPassword(encodepass);
        }
    }

    /**
     * 登录日志
     * @param joinPoint
     */
    @After("execution(* com.yurendao.shop.user.service.*.login*(..))")
    public void login(JoinPoint joinPoint){
        if(joinPoint.getArgs()[0] instanceof User){
            User user = (User) joinPoint.getArgs()[0];
            logger.info("用户登录:"+user.getNickname());
        }
    }


}
